#ifndef BACKUPTASK_H
#define BACKUPTASK_H

#include <QtGui>
#include <QObject>
#include <QMetaType>
#include <QSqlQuery>
#include "fileutils.h"

class BackupTask
{
public:

    enum TaskType {
        Backup = 1,
        Restore = 2
    };

    enum BackupType {
        Complete,
        Incremental,
        Differential
    };

    enum TargetType {
        Local,
        Ftp,
        Ssh
    };

    enum VerificationType {
        Hash,
        Size
    };

    enum CompressionType {
        NoCompression,
        Fastest,
        Medium,
        Maximum
    };

    BackupTask();
    BackupTask(quint32 taskId);

    ~BackupTask();

    /* Setters */
    inline void setSourcePath(QString sPath) {sourcePath = sPath;}
    inline void setTargetPath(QString tPath) {targetPath = tPath;}
    inline void setBackupType(BackupType bType) {backupType = bType;}
    inline void setVerificationType(VerificationType vType) {verificationType = vType;}
    inline void setBackupTargetType(TargetType tType) {targetType = tType;}
    inline void setCompressionType(CompressionType cType) {compressionType = cType;}
    void setRemoteOptions(QString host, QString login, QString pass, QString dir);
    inline void setArchived(bool value) {archived = value;}
    inline void setPeriodic(bool periodic) {this->periodic = periodic;}
    inline void setNextRun(QDateTime nextRun) {this->nextRun = nextRun;}
    inline void setInterval(quint32 interval) {this->interval = interval;}
    void SetupNewNextRun();

    /* Getters */
    inline BackupTask::TaskType getTaskType() const {return taskType;}
    inline quint32 getTaskId() const {return taskId;}
    inline BackupType getBackupType() const {return backupType;}
    inline TargetType getTargetType() const {return targetType;}
    inline VerificationType getVerificationType() const {return verificationType;}
    inline CompressionType getCompressionType() const {return compressionType;}
    inline bool getArchived() const {return archived;}
    inline bool getPeriodic() const {return periodic;}
    inline QString getSourcePath() const {return sourcePath;}
    inline QString getTargetPath() const {return targetPath;}
    inline quint32 getInterval() const {return interval;}
    inline QDateTime getNextRun() const {return nextRun;}
    inline QString getHost() const {return remoteHost;}
    inline QString getLogin() const {return remoteLogin;}
    inline QString getPassword() const {return remotePassword;}
    inline QString getTargetDir() const {return remoteTargetDir;}

    /* De/serializers */
    void serialize(QDataStream &out);
    void unserialize(QDataStream &in);

    /* DB manipulation */
    void createTask();
    void updateTask();
    void deleteTask();
    void saveInstance(QDateTime timestamp, quint32 instance_parent);
    void deleteInstance(QDateTime timestamp);
    QList<QDateTime> getInstances();
    quint32 getInstanceParent(QDateTime timestamp);
    void setInstanceSize(QDateTime timestamp, quint64 size);
    quint64 getInstanceSize(QDateTime timestamp);
    bool instanceHasChildren(QDateTime timestamp);

protected:
    TaskType taskType;

private:
    quint32 taskId;

    BackupType backupType;
    TargetType targetType;
    VerificationType verificationType;
    CompressionType compressionType;
    bool archived;
    bool periodic;

    QString sourcePath;
    QString targetPath;

    QString remoteHost;
    QString remoteLogin;
    QString remotePassword;
    QString remoteTargetDir;

    QDateTime nextRun;
    quint32 interval;
};

Q_DECLARE_METATYPE(QSharedPointer<BackupTask>)

#endif // BACKUPTASK_H
